package org.eclipseuseradmin.internal.ui.editors;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipseuseradmin.ui.utils.DialogSupport;

public class ChangePasswordDialog extends Dialog {

	private Label passwordLabel;
	private Label confirmPasswordLabel;
	private Text passwordText;
	private Text confirmPasswordText;
	private Text errorMessageText;
	private String password;

	private DialogSupport dialogSupport;

	public ChangePasswordDialog(final Shell parent) {
		super(parent);
	}

	protected Control createDialogArea(final Composite parent) {
		// create composite
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
		GridData layoutData = new GridData(GridData.GRAB_HORIZONTAL
				| GridData.HORIZONTAL_ALIGN_FILL);
		layoutData.horizontalSpan = 2;
		errorMessageText.setLayoutData(layoutData);
		errorMessageText.setBackground(errorMessageText.getDisplay()
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		errorMessageText.setForeground(errorMessageText.getDisplay()
				.getSystemColor(SWT.COLOR_RED));
		dialogSupport = new DialogSupport(errorMessageText);

		passwordLabel = new Label(composite, SWT.NONE);
		passwordLabel
				.setText(EditorsMessages.get().ChangePasswordDialog_password);
		passwordText = new Text(composite, SWT.BORDER);
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		passwordText.setLayoutData(layoutData);
		passwordText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent event) {
				validatePassword();
			}
		});

		confirmPasswordLabel = new Label(composite, SWT.NONE);
		confirmPasswordLabel
				.setText(EditorsMessages.get().ChangePasswordDialog_confirmPassword);
		confirmPasswordText = new Text(composite, SWT.BORDER);
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		confirmPasswordText.setLayoutData(layoutData);
		confirmPasswordText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent event) {
				validateConfirmPassword();
			}
		});

		return composite;
	}

	@Override
	protected Control createContents(Composite parent) {
		Control control = super.createContents(parent);

		validatePassword();
		validateConfirmPassword();
		errorMessageText.setText("");

		return control;
	}

	protected void configureShell(final Shell shell) {
		super.configureShell(shell);
		shell.setText(EditorsMessages.get().ChangePasswordDialog_title);
	}

	@Override
	protected void okPressed() {

		password = passwordText.getText();
		super.okPressed();
	}

	protected void validatePassword() {

		String errorMessage = null;
		String password = passwordText.getText();

		if (password.length() == 0) {
			errorMessage = EditorsMessages.get().ChangePasswordDialog_error_password_empty;
		} else if (password.length() < 5) {
			errorMessage = EditorsMessages.get().ChangePasswordDialog_error_password_length;
		}

		setErrorMessage(passwordText, errorMessage);
	}

	protected void validateConfirmPassword() {

		String errorMessage = null;
		String password = confirmPasswordText.getText();

		if (password.length() == 0) {
			errorMessage = EditorsMessages.get().ChangePasswordDialog_error_confirmPassword_empty;
		} else if (!password.equals(passwordText.getText())) {
			errorMessage = EditorsMessages.get().ChangePasswordDialog_error_confirmPassword_match;
		}

		setErrorMessage(confirmPasswordText, errorMessage);
	}

	private void setErrorMessage(Object key, String message) {

		if (message == null) {
			dialogSupport.removeMessage(key);
		} else {
			dialogSupport.addMessage(key, message);
		}

		Control button = getButton(IDialogConstants.OK_ID);
		if (button != null) {
			button.setEnabled(!dialogSupport.isMessage());
		}
	}

	public String getPassword() {
		return password;
	}

}
